<!DOCTYPE html>
<html lang="en">
<title>企业储值卡管理</title>
<#import "/layout/layout.html" as body/>

<@body.page>
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>企业储值卡管理</h2>
            <ol class="breadcrumb">
                <li><a href="#">企业储值账户列表</a></li>
                <#if company?? && company.status == 1>
                    <li class="active"><strong>编辑企业</strong></li>
                    <#else>
                        <li class="active"><strong>新增企业</strong></li>
                </#if>
            </ol>
        </div>
    </div>
    <div class="wrapper wrapper-content animated fadeInRight">

        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title overflow">
                        <h5 class="pull-left"></h5>
                        <#if couponInfo??>
                            <h5 class="pull-left">编辑企业</h5>
                            <#else>
                                <h5 class="pull-left">新增企业</h5>
                        </#if>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal" id="addForm">
                            <input type="hidden" id="id" name="id" value="${(company.id)!}">
                            <input type="hidden" id="status" name="status" value="${(company.status)!}">
                            <input type="hidden" id="version" name="version" value="${(company.version)!}">

                            <div class="form-group"><h2 class="col-sm-12 margin-top">Step1:配置基础信息</h2></div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">企业名称</label>
                                <div class="col-sm-10">
                                    <input type="text" id="name" name="name" value="${(company.name)!}" class="form-control" placeholder="请输入企业客户名称">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">企业管理员</label>
                                <div class="col-sm-10">
                                    <input type="text" id="manager" name="manager" value="${(company.manager)!}" class="form-control" placeholder="请输入企业管理员姓名">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">企业管理员电话</label>
                                <div class="col-sm-10">
                                    <input type="text" id="managerMobile" name="managerMobile" value="${(company.managerMobile)!}" class="form-control" placeholder="请输入企业管理员手机号码">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group"><label class="col-sm-2 control-label">客户经理</label>
                                <div class="col-sm-10" id="clientManager">
                                    <#if checkList??>
                                        <#list checkList as list>
                                            <div class="checkbox-inline i-checks"><label for="${list.id!}"> <input type="radio" id="${list.id!}" value="${list.name!}" data-mobile="${list.phone!}" name="clientManager" <#if company?? && company.clientManager=list.name>checked</#if> > <i></i> ${list.name!} </label></div>
                                        </#list>
                                    </#if>
                                </div>
                            </div>

                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">客户经理电话</label>
                                <div class="col-sm-10">
                                    <input type="hidden"  id="clientManagerMobileHide" name="clientManagerMobile" value="${(company.clientManagerMobile)!}">
                                    <div id="clientManagerMobile" style="margin-top: 8px"></div>
                                </div>
                            </div>

                            <div class="hr-line-dashed"></div>
                            <div class="form-group"><label class="col-sm-2 control-label">限制员工默认每月限制</label>
                                <div class="col-sm-10" id="isMonthlyLimit">
                                    <div class="checkbox-inline i-checks"><label for="defaultYes"> <input type="radio" id="defaultYes" value="0" name="isMonthlyLimit" <#if company?? && company.isMonthlyLimit=0>checked</#if>  > <i></i> 限制 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="defaultNo"> <input type="radio" id="defaultNo" value="1" name="isMonthlyLimit" <#if company?? && company.isMonthlyLimit=1>checked</#if> > <i></i> 不限 </label></div>
                                    <small style="color: mediumseagreen;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(此字段仅供新增员工填充默认值使用 对企业无效)</small>
                                </div>
                            </div>

                            <div class="hr-line-dashed time_limit"></div>
                            <div class="form-group time_limit">
                                <label class="col-sm-2 control-label">员工默认每月限额</label>
                                <div class="col-sm-10">
                                    <input type="number" id="monthlyLimit" name="monthlyLimit" value="${(company.monthlyLimit)!}" min="0" class="form-control" placeholder="请输入新增员工时的默认每月限额">
                                </div>
                            </div>

                            <div class="hr-line-dashed"></div>
                            <div class="form-group"><label class="col-sm-2 control-label">限制企业每月总限额</label>
                                <div class="col-sm-10" id="isMonthlyTotalLimit">
                                    <div class="checkbox-inline i-checks"><label for="defaultTotalYes"> <input type="radio" id="defaultTotalYes" value="0" name="isMonthlyTotalLimit" <#if company?? && company.isMonthlyTotalLimit=0>checked</#if>  > <i></i> 限制 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="defaultTotalNo"> <input type="radio" id="defaultTotalNo" value="1" name="isMonthlyTotalLimit" <#if company?? && company.isMonthlyTotalLimit=1>checked</#if> > <i></i> 不限 </label></div>
                                </div>
                            </div>

                            <div class="hr-line-dashed total_limit"></div>
                            <div class="form-group total_limit">
                                <label class="col-sm-2 control-label">企业每月总限额</label>
                                <div class="col-sm-10">
                                    <input type="number" id="monthlyTotalLimit" name="monthlyTotalLimit" value="${(company.monthlyTotalLimit)!}" min="0" class="form-control" placeholder="请输入企业每月总限额">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group"><label class="col-sm-2 control-label">限制员工默认可买类型</label>
                                <div class="col-sm-10" id="setDefaultType">
                                    <div class="checkbox-inline i-checks"><label for="setYes"> <input type="radio" id="setYes" value="0" name="isBuyTypeLimit" <#if company?? && company.isBuyTypeLimit=0>checked</#if> > <i></i> 限制 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="setNo"> <input type="radio" id="setNo" value="1" name="isBuyTypeLimit" <#if company?? && company.isBuyTypeLimit=1>checked</#if> > <i></i> 不限 </label></div>
                                </div>
                            </div>
                            <div class="hr-line-dashed type-limit"></div>
                            <div class="form-group type-limit"><label class="col-sm-2 control-label">员工默认可买类型</label>
                                <div class="col-sm-10" id="DefaultType">
                                    <div class="checkbox-inline i-checks"><label for="team"> <input type="checkbox" id="team" value="1" name="limitTeamCourse" <#if company?? && company.limitTeamCourse?? && company.limitTeamCourse=1>checked</#if> > <i></i> 团课 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="personal"> <input type="checkbox" id="personal" value="1" name="limitSecretCourse" <#if company?? && company.limitSecretCourse?? && company.limitSecretCourse=1>checked</#if>  > <i></i> 私教 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="camp"> <input type="checkbox" id="camp" value="1" name="limitCampCourse" <#if company?? && company.limitCampCourse?? && company.limitCampCourse=1>checked</#if> > <i></i> 训练营 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="self"> <input type="checkbox" id="self" value="1" name="limitSelfCourse" <#if company?? && company.limitSelfCourse?? && company.limitSelfCourse=1>checked</#if>  > <i></i> 自助 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="vip"> <input type="checkbox" id="vip" value="1" name="limitVip" <#if company?? && company.limitVip?? && company.limitVip=1>checked</#if> > <i></i> 会员 </label></div>
                                    <!--<div class="checkbox-inline i-checks"><label for="card"> <input type="checkbox" id="card" value="1" name="limitValuedCard" <#if company?? && company.limitValuedCard?? && company.limitValuedCard=1>checked</#if> > <i></i> 储值卡 </label></div>-->
                                </div>
                            </div>

                            <div class="hr-line-dashed  course-limit "></div>
                            <div class="form-group course-limit"><label class="col-sm-2 control-label">是否限制可使用门店</label>
                                <div class="col-sm-10" id="setStoreLimitType">
                                    <div class="checkbox-inline i-checks"><label for="setStoreLimitYes"> <input type="radio" id="setStoreLimitYes" value="0" name="isStoreListLimit" <#if company?? && company.isStoreListLimit?? && company.isStoreListLimit=0>checked</#if> > <i></i> 限制 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="setStoreLimitNo"> <input type="radio" id="setStoreLimitNo" value="1" name="isStoreListLimit" <#if company?? && company.isStoreListLimit?? && company.isStoreListLimit=1>checked</#if> > <i></i> 不限 </label></div>
                                </div>
                            </div>

                            <div class="hr-line-dashed store-limit"></div>
                            <div class="form-group store-limit"><label class="col-sm-2 control-label">选择可使用的门店</label>
                                <div class="col-sm-10" id="storeTypeLimit">
                                    <#if storeList??>
                                        <#list storeList as store>
                                            <div class="checkbox-inline i-checks"><label for="store-limit-${store.id}"> <input type="checkbox" id="store-limit-${store.id}"  value="${store.id}" name="storeListLimit"> ${store.storeName} </label></div>
                                        </#list>
                                    </#if>
                                </div>
                            </div>

                            <div class="hr-line-dashed"></div>
                            <div class="form-group"><label class="col-sm-2 control-label">结算类型</label>
                                <div class="col-sm-10" id="setCheckType">
                                    <div class="checkbox-inline i-checks"><label for="setCheckYes"> <input type="radio" id="setCheckYes" value="0" name="vipCheckType" <#if company?? && company.vipCheckType=0>checked</#if> > <i></i> 按会员价 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="setCheckNo"> <input type="radio" id="setCheckNo" value="1" name="vipCheckType" <#if company?? && company.vipCheckType=1>checked</#if> > <i></i> 按原价 </label></div>
                                </div>
                            </div>

                            <div class="hr-line-dashed  vip-course-limit "></div>
                            <div class="form-group vip-course-limit vip-course-limit"><label class="col-sm-2 control-label">是否限制会员价格多次购买</label>
                                <div class="col-sm-10" id="setBuyType">
                                    <div class="checkbox-inline i-checks"><label for="setBuyYes"> <input type="radio" id="setBuyYes" value="0" name="vipCheckVipPriceOrderMany" <#if company?? && company.vipCheckVipPriceOrderMany?? && company.vipCheckVipPriceOrderMany=0>checked</#if> > <i></i> 限制 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="setBuyNo"> <input type="radio" id="setBuyNo" value="1" name="vipCheckVipPriceOrderMany" <#if company?? && company.vipCheckVipPriceOrderMany?? && company.vipCheckVipPriceOrderMany=1>checked</#if> > <i></i> 不限 </label></div>
                                </div>
                            </div>


                            <div class="hr-line-dashed  vip-course-limit "></div>
                            <div class="form-group vip-course-limit vip-course-limit"><label class="col-sm-2 control-label">是否限制会员价可使用门店</label>
                                <div class="col-sm-10" id="setStoreType">
                                    <div class="checkbox-inline i-checks"><label for="setStoreYes"> <input type="radio" id="setStoreYes" value="0" name="vipCheckStoreLimit" <#if company?? && company.vipCheckStoreLimit?? && company.vipCheckStoreLimit=0>checked</#if> > <i></i> 限制 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="setStoreNo"> <input type="radio" id="setStoreNo" value="1" name="vipCheckStoreLimit" <#if company?? && company.vipCheckStoreLimit?? && company.vipCheckStoreLimit=1>checked</#if> > <i></i> 不限 </label></div>
                                </div>
                            </div>

                            <div class="hr-line-dashed vip-store-limit"></div>
                            <div class="form-group vip-course-limit  vip-store-limit"><label class="col-sm-2 control-label">选择会员价可使用的门店</label>
                                <div class="col-sm-10" id="storeType">
                                    <#if storeList??>
                                        <#list storeList as store>
                                            <div class="checkbox-inline i-checks"><label for="store-${store.id}"> <input type="checkbox" id="store-${store.id}"  value="${store.id}" name="vipCheckStoreList"> ${store.storeName} </label></div>
                                        </#list>
                                    </#if>
                                </div>
                            </div>

                            <div class="hr-line-dashed vip-course-limit"></div>
                            <div class="form-group vip-course-limit"><label class="col-sm-2 control-label">限制以会员价结算的课程类型</label>
                                <div class="col-sm-10" id="setVipCourseType">
                                    <div class="checkbox-inline i-checks"><label for="setVipCourseYes"> <input type="radio" id="setVipCourseYes" value="0" name="vipCheckCourseTypeLimit" <#if company?? && company.vipCheckCourseTypeLimit?? && company.vipCheckCourseTypeLimit=0>checked</#if> > <i></i> 限制 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="setVipCourseNo"> <input type="radio" id="setVipCourseNo" value="1" name="vipCheckCourseTypeLimit" <#if company?? && company.vipCheckCourseTypeLimit?? && company.vipCheckCourseTypeLimit=1>checked</#if> > <i></i> 不限 </label></div>
                                </div>
                            </div>

                            <div class="hr-line-dashed vip-course-limit"></div>
                            <div class="form-group vip-course-limit vip-limit"><label class="col-sm-2 control-label">会员价结算课程类型</label>
                                <div class="col-sm-10" id="vipCourseType">
                                    <div class="checkbox-inline i-checks"><label for="vipTeam"> <input type="checkbox" id="vipTeam" value="1" name="vipCheckCourseTypeTeam" <#if company?? && company.vipCheckCourseTypeTeam?? && company.vipCheckCourseTypeTeam=1>checked</#if> > <i></i> 团课 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="vipPersonal"> <input type="checkbox" id="vipPersonal" value="1" name="vipCheckCourseTypePrivate" <#if company?? && company.vipCheckCourseTypePrivate?? && company.vipCheckCourseTypePrivate=1>checked</#if>  > <i></i> 私教 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="vipCamp"> <input type="checkbox" id="vipCamp" value="1" name="vipCheckCourseTypeCamp" <#if company?? && company.vipCheckCourseTypeCamp?? && company.vipCheckCourseTypeCamp=1>checked</#if> > <i></i> 训练营 </label></div>
                                    <div class="checkbox-inline i-checks"><label for="vipSelf"> <input type="checkbox" id="vipSelf" value="1" name="vipCheckCourseTypeSelf" <#if company?? && company.vipCheckCourseTypeSelf?? && company.vipCheckCourseTypeSelf=1>checked</#if>  > <i></i> 自助 </label></div>
                                    <!--<div class="checkbox-inline i-checks"><label for="card"> <input type="checkbox" id="card" value="1" name="limitValuedCard" <#if company?? && company.limitValuedCard?? && company.limitValuedCard=1>checked</#if> > <i></i> 储值卡 </label></div>-->
                                </div>
                            </div>

                            <div class="hr-line-dashed type-limit vip-course-limit vip-limit"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">备注</label>
                                <div class="col-sm-10">
                                    <input type="text" id="remark" name="remark" value="${(company.remark)!}" class="form-control" placeholder="非必填，可输入相关备注信息">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="ibox-content overflow">
                        <div class="col-sm-12">

                            <#if company?? && company.status == 1>
                                <a id="update_go_back_list" href="${baseUrl}system/company/list"  class="btn btn-default pull-left">返回列表</a>
                                <#else>
                                    <a id="go_back_list" href="${baseUrl}system/company/list"  class="btn btn-default pull-left">取消添加</a>
                            </#if>
                            <#if company?? && company.status == 1>
                                <input type="hidden" id="companyBindingUrl" value="${companyBindingUrl}" />
                                <input type="hidden" id="companyAmount" value="${companyAmount}" />
                                <a id="company_update" href="#" class="btn btn-primary pull-right">更新</a>
                                <button id="company_download_qr_code" type="button" class="btn pull-right btn-info" style="margin-right: 15px">下载企业二维码</button>
                                <#else>
                                    <a id="company_add_step2" href="#" class="btn btn-primary pull-right">保存并进入下一步</a>
                            </#if>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div style="display: none;" id="testDiv">
            <div id="qrcode"></div>
            <a id="download" download="qrcode.jpg"></a>
            <button id="save">save</button>
        </div>
</div>
<script src="${baseUrl}js/channel/jquery.qrcode.min.js"></script>
<script>
    $(document).ready(function() {
        $('#storeType input').iCheck('disable');

        $("#setCheckType input").each(function (index, item) {
            if(index == 1 && item.checked == true) {
                $('.vip-course-limit').hide(400);
                return false;
            }
        });
        $('#setCheckType input').on('ifChecked', function () {
            if ($(this).val() === '0') {
                $('.vip-course-limit').show(400);
            } else {
                $('.vip-course-limit').hide(400);
            }
        });
        $("#setStoreType input").each(function (index, item) {
            if(index == 1 && item.checked == true) {
                $('.vip-store-limit').hide(400);
                return false;
            }
        });
        $('#setStoreType input').on('ifChecked', function () {
            if ($(this).val() === '0') {
                $('.vip-store-limit').show(400);
            } else {
                $('.vip-store-limit').hide(400);
            }
        });
        $('#setStoreLimitType input').on('ifChecked', function () {
            if ($(this).val() === '0') {
                $('.store-limit').show(400);
                $('#storeType .icheckbox_square-green').iCheck('uncheck').iCheck('disable');
                $("#storeTypeLimit input").each(function (index, item) {
                    if (item.checked == true) {
                        $('#storeType input').eq(index).iCheck('enable');
                    }
                });
            } else {
                $('.store-limit').hide(400);
                $('#storeType .icheckbox_square-green').iCheck('enable');
            }
        });
        $("#setStoreLimitType input").each(function (index, item) {
            if(index == 1 && item.checked == true) {
                $('.store-limit').hide(400);
                $('#storeType input').iCheck('enable');
                return false;
            }
        });
        $("#setVipCourseType input").each(function (index, item) {
            if(index == 0 && item.checked == true) {
                $('#vipSelf').parent().iCheck('disable');
                $('#vipSelf').iCheck('disable');
                return false;
            } else if(index == 1 && item.checked == true) {
                $('.vip-limit').hide(400);
            }
        });
        $('#setVipCourseType input').on('ifChecked', function () {
            if ($(this).val() === '0') {
                $('#vipSelf').parent().iCheck('disable');
                $('#vipSelf').iCheck('disable');
                $('.vip-limit').show(400);
            } else {
                $('.vip-limit').hide(400);
            }
        });
        var company = ${companyJson!'null'};
        if(company){

            if(company.isBuyTypeLimit == 1) {
                $('.type-limit').hide();
            }
            if(company.isMonthlyLimit== 1) {
                $('.time_limit').hide();
            }
            if(company.isMonthlyTotalLimit== 1) {
                $('.total_limit').hide();
            }
            $("#clientManagerMobile").html($("#clientManagerMobileHide").val());
            if (company.storeListLimit) {
                $(company.storeListLimit.split("#")).each(function (i, data) {
                    $("input[name='storeListLimit']").each(function(i,store){
                        if (data == store.value) {
                            $("#store-limit-"+store.value).prop('checked',true);
                        }
                    });
                });
            }
            if (company.vipCheckStoreList) {
                $(company.vipCheckStoreList.split("#")).each(function (i, data) {
                    $("input[name='vipCheckStoreList']").each(function(i,store){
                        if (data == store.value) {
                            $("#store-"+store.value).prop('checked',true);
                        }
                    });
                });
            }

        }


        $("#storeTypeLimit input").each(function (index, item) {
            if(item.checked == true) {
                $('#storeType input').eq(index).iCheck('enable');
            }
            $(this).on('ifChecked', function () {
                $('#storeType .icheckbox_square-green').eq(index).iCheck('enable');
            });
            $(this).on('ifUnchecked', function () {
                $('#storeType .icheckbox_square-green').eq(index).removeClass('checked').iCheck('disable');
            });
        });

        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });

        $('#isMonthlyLimit input').on('ifChecked', function () {
            if ($(this).val() === '0') {
                $('.time_limit').show(200);
            } else {
                $('.time_limit').hide(200);
            }
        });

        $('#isMonthlyTotalLimit input').on('ifChecked', function () {
            if ($(this).val() === '0') {
                $('.total_limit').show(200);
            } else {
                $('.total_limit').hide(200);
            }
        });

        $('#setDefaultType input').on('ifChecked', function () {
            if ($(this).val() === '0') {
                $('.type-limit').show(200);
            } else {
                $('.type-limit').hide(200);
            }
        });
        $('#clientManager input').on('ifChecked', function () {
            $('#clientManagerMobile').text($(this).attr('data-mobile'));
            $('#clientManagerMobileHide').val($(this).attr('data-mobile'));
        });

        $("#company_add_step2, #company_update").click(function () {
            save_company_step1();
        })
    });


    /**
     * 企业保存step1/更新
     *
     */
    function save_company_step1() {
        var param = $("#addForm").serialize();
        $.post("${baseUrl}system/company/save_company_step1", param, function (result) {
            result = JSON.parse(result);
            if(result && result.go =="list"){
                swal("更新成功!", "页面即将重新加载,请稍后...", "success");
                window.setTimeout(function () {
                    window.location = "${baseUrl}system/company/list"
                }, 1000);
            } else if(result && result.success){
                debugger
                swal("保存成功!", "页面即将重新加载,请稍后...", "success");
                window.setTimeout(function () {
                    window.location = '${baseUrl}system/company/add_step2?id=' + result.id
                }, 1000);
            } else {
                swal("保存失败!", result.msg, "error");
            }
        });
    }

    /**
     * 下载企业二维码
     *
     */
    $("#company_download_qr_code").click(function () {
        var companyAmount = $("#companyAmount").val();
        if (companyAmount <= 0) {
            swal("企业余额不足", "请先进行充值后再试!", "error");
            return false;
        }


        var url = $("#companyBindingUrl").val();
        downloadImg(url, $("#name").val());
    });

    function downloadImg(url,fileName){
//        var urls = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='+url;
        fileName= fileName+".jpg";
        $("#testDiv").empty();
        html = '<div id="qrcode"></div><a id="download" download="qrcode.jpg"></a><button id="save">save</button>';
        $("#testDiv").append(html);
        jQuery('#qrcode').qrcode({width: 500,height: 500,text: url});
        var canvas = $('#qrcode').find("canvas").get(0);
        var urlnew = canvas.toDataURL(url);
        $("#download").attr('download',fileName).click();
        $("#download").attr('href', "").click();
        $("#download").attr('href', urlnew).get(0).click();
        $("#testDiv").empty();
        return false;
    }
</script>
</@body.page>
</html>